﻿var pdb; //Pdb reference

Ext.define('Sofia.controller.File',
{    
    extend: 'Ext.app.Controller',
    //stores: ['Pdbs'],
    models: ['Pdb'],
    views: ['Sidebar', 'tab.File'],

    refs:
    [{
        selector: '.sidebar',
        ref: 'sideBar'
    }],

    init: function () {
        this.control(
        {
            '#HeaderPanel > #FileButton':
            {
                click: this.onFileClick
            },
            'fileTab #browseButton':
            {
                change: this.onBrowseChange
            },
            'fileTab #loadButton':
            {
                click: this.onLoadClick
            },
            '#RunEuler':
            {
                click: this.onRunEulerClick
            }
        });
    },

    onFileClick: function () {
        var sideBar = this.getSideBar();
        sideBar.setActiveTab(0); //File tab
    },

    onBrowseChange: function (fb, v) {
        Ext.get('selectedPDBlabel').update(v);
    },

    onLoadClick: function () {
        var file = Ext.getCmp('browseButton').getEl().down('input[type=file]').dom.files[0];
        var reader = new FileReader();
        reader.onload = (function (theFile) {
            return function (e) {
                rawtext = e.target.result;

                var reader = Ext.create('Sofia.file.PDBReader');
                pdb = reader.parsePDB(rawtext);

                drawAtoms(pdb);

            };
        })(file);
        reader.readAsText(file);
    },
    onRunEulerClick: function () 
    {
        if (pdb != undefined)
        {
            eulerIntegrate(pdb, 10);
        }
    }
});